package com.ruoyi.car.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.car.controller.admin.CarCustomer.vo.CarCustomerReq;
import com.ruoyi.car.controller.admin.CarCustomer.vo.CarCustomerResp;
import com.ruoyi.car.entity.CarCustomer;
import com.ruoyi.car.service.CarCustomerService;
import com.ruoyi.car.mapper.CarCustomerMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
* @author 11452
* @description 针对表【car_customer(客户信息表)】的数据库操作Service实现
* @createDate 2023-06-06 11:24:14
*/
@Service
public class CarCustomerServiceImpl extends ServiceImpl<CarCustomerMapper, CarCustomer>
    implements CarCustomerService{

    @Autowired
    private CarCustomerMapper carCustomerMapper;

    @Override
    public PageInfo<CarCustomerResp> search(CarCustomerReq req) {
        return PageHelper.startPage(req.getPage(),req.getSize()).doSelectPageInfo(()->{
            //多表操作，自己写sql
            carCustomerMapper.search(req);
        });
    }

    @Override
    public CarCustomerResp findById(Long id) {
        return carCustomerMapper.findById(id);
    }

    @Override
    public int checkPhoneUnique(Long id, String phone) {
        LambdaQueryWrapper<CarCustomer> queryWrapper=new LambdaQueryWrapper<>();
        //select count(1) from 表  where phone=值
        queryWrapper.eq(CarCustomer::getPhone,phone);
        if(Objects.nonNull(id)){
            // and  id!=?
            queryWrapper.ne(CarCustomer::getId,id);
        }
        return  carCustomerMapper.selectCount(queryWrapper).intValue();
    }
}




